#include <retro.h>
#include "retro.creator.h"

using namespace Retro::Creator::Dialog;
using namespace SPLib::Windows;

#define RETRO_PROJECTPATH_ID    0x0100
#define RETRO_PROJECTNAME_ID    0x0200

TCHAR gameSizes[][20] = {
  _T("640x480"),
  _T("800x600 (Default)"),
  _T("1024x768")
};

INT gameSizesVal[][2] = {
  { 640, 480 },
  { 800, 600 },
  { 1024, 768 }
};

VOID    EditProject::formatPath()
{
  PTCHAR last = _tcsrchr(path, _T('\\'));

  if (last)
  {
    ++last;
  }
  else
  {
    _tcscat(path, _T("\\"));
    last = _tcsrchr(path, _T('\\')) + 1;
  }

  GetWindowText(hEditName, last, (INT)(MAX_PATH - (last - path)));
  SetWindowText(hEditPath, path);
}

VOID    EditProject::onUnload()
{
}

VOID    EditProject::onLoad()
{
  path[0] = '\0';

  memset(&brwPath, 0, sizeof(brwPath));

  brwPath.hwndOwner       = *this;
  brwPath.pszDisplayName  = path;
  brwPath.lpszTitle       = _T("Please select your project directory...");
  brwPath.ulFlags         = BIF_USENEWUI | BIF_RETURNONLYFSDIRS |
    BIF_DONTGOBELOWDOMAIN;

  addEdit(hEditName, lblName, _T("Name:"));
  addPath(hEditPath, lblPath, btnPath, _T("Path:"), RETRO_PROJECTPATH_ID);
  addSeparator(sepName);

  SetWindowLongPtr(hEditName, GWLP_ID, RETRO_PROJECTNAME_ID);

  addComboBox(hCombSize, lblSize, _T("Game Size:"));
  ComboBox_AddStrings(hCombSize, gameSizes);

  addOKCancel(btnOK, btnCancel, sepButtons);
}

VOID    EditProject::onCommand(Event::CommandEvent& iEvent)
{
  switch (iEvent.id)
  {
  case RETRO_PROJECTNAME_ID:
    if (iEvent.code == EN_CHANGE && isNew)
    {
      GetWindowText(hEditPath, path, MAX_PATH);
      formatPath();
    }
    break;
  }
}

VOID EditProject::onNotify(Event::NotifyEvent& iEvent)
{
  switch (iEvent.data->idFrom)
  {
  case RETRO_PROJECTPATH_ID:
    {
      HWND focus = GetFocus();
      PIDLIST_ABSOLUTE result = SHBrowseForFolder(&brwPath);

      if (result)
      {
        SHGetPathFromIDList(result, path);
        CoTaskMemFree(result);
        _tcscat_s(path, MAX_PATH, _T("\\"));

        formatPath();
      }

      SetFocus(focus);
    }
    break;
  case IDOK:
    if (isNew)
    {
      GetWindowText(hEditPath, path, MAX_PATH);
      CreateDirectory(path, NULL);

      if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES)
      {
        MessageBox(*this, _T("Invalid project path"),
          NULL, MB_ICONERROR);
        break;
      }
    }

    if (GetWindowTextLength(hEditName) == 0)
    {
      MessageBox(*this, _T("Project name cannot be empty"),
        NULL, MB_ICONERROR);
      break;
    }

    setResult(DialogOK);
  case IDCANCEL:
    hide();
    break;
  }
}

VOID    EditProject::newProject()
{
  isNew = TRUE;
  setText(_T("New Project"));

  ComboBox_SetCurSel(hCombSize, 1);
  Edit_SetText(hEditName, _T("New Project"));

  SHGetSpecialFolderPath(*this, path, CSIDL_MYDOCUMENTS, FALSE);

  _tcscat_s(path, MAX_PATH, _T("\\"));
  formatPath();

  Edit_SetReadOnly(hEditPath, FALSE);
  EnableWindow(btnPath, TRUE);
}

VOID    EditProject::setProject(Data::RetroProject* value)
{
  INT sizeIndex = 1;

  isNew = FALSE;
  setText(_T("Project Properties"));

  Array_ForEachI(i, gameSizesVal)
  {
    if (value->getGameWidth() <= gameSizesVal[i][0])
    {
      sizeIndex = i;
      break;
    }
  }

  ComboBox_SetCurSel(hCombSize, sizeIndex);

  GetCurrentDirectory(MAX_PATH, path);
  Edit_SetText(hEditPath, path);
  Edit_SetText(hEditName, value->getName());

  Edit_SetReadOnly(hEditPath, TRUE);
  EnableWindow(btnPath, FALSE);
}

VOID    EditProject::getProject(Data::RetroProject* value)
{
  TCHAR szName[RETRO_MAXNAME];
  LRESULT size = ComboBox_GetCurSel(hCombSize);

  Edit_GetText(hEditName, szName, RETRO_MAXNAME);

  value->setName(szName);
  value->setGameWidth(gameSizesVal[size][0]);
  value->setGameHeight(gameSizesVal[size][1]);
}
